package com.accounting.client.frontend.product.tree;

import java.util.List;

import com.accounting.client.remote.obj.ProductCatalog;
import com.google.gwt.dom.client.Style.TextAlign;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.delegate.DataCallback;
import corelib.client.resource.CommonResources;
import corelib.client.ui.LazyWidget;
import corelib.client.ui.widget.form.LabelWidget;
import corelib.client.ui.widget.panel.SimpleLayoutPanel;
import corelib.client.ui.widget.tree.SimpleTree;
import corelib.client.ui.widget.tree.SimpleTreeHandler;
import corelib.client.ui.widget.tree.SimpleTreeNode;

public class ProductCatalogTree extends LazyWidget<Widget> {
	
	private SimpleTree tree;
	private ScrollPanel scrollPanel;
	
	private ProductCatalog selectedCatalogInfo;
	private DataCallback<ProductCatalog> action;
	
	public ProductCatalogTree(DataCallback<ProductCatalog> action){
		this.action = action;
	}
	
	public DataCallback<ProductCatalog> getAction() {
		return this.action;
	}
	
	public void setAction(DataCallback<ProductCatalog> action){
		if (action != null){
			this.action = action;
		}
	}
	
	@Override
	protected Widget createWidget() {
		SimpleLayoutPanel panel = new SimpleLayoutPanel();
		panel.getElement().getStyle().clearOverflow();
		
		SimplePanel col = new SimplePanel();
		col.setHeight("27px");
		col.addStyleName(CommonResources.INSTANCE.css().panelHeader());
		col.add(new LabelWidget("||产品分类").setBold(true).setAlign(TextAlign.LEFT));
		
		scrollPanel = new ScrollPanel();
		scrollPanel.getElement().getStyle().setBackgroundColor("#fff");
		
		tree = createTree();
		scrollPanel.add(tree);
		
		panel.add(col);
		panel.add(scrollPanel);
		
		panel.setWidgetTopHeight(col, 0, Unit.PX, 26, Unit.PX);
		panel.setWidgetTopBottom(scrollPanel, 26, Unit.PX, 0, Unit.PX);
		return panel;
	}
	
	public SimpleTree getTree() {
		return this.tree;
	}
	
	public ProductCatalog getSelectedCatalog() {
		return this.selectedCatalogInfo;
	}

	public void setSelectedCalalog(ProductCatalog catalogInfo) {
		if (this.selectedCatalogInfo != catalogInfo){
			this.selectedCatalogInfo  = catalogInfo;
		}
	}
	
	public void clear() {
		this.selectedCatalogInfo = null;
		List<SimpleTreeNode> nodes = getTree().getAllNodes();
		for (SimpleTreeNode simpleCellTreeNode : nodes) {
			 getTree().remove(simpleCellTreeNode);
		}
	}
	
	protected SimpleTree createTree() {
		return new SimpleTree(new ProductCatalogTreeHandler(this));
	}
	
	private class ProductCatalogTreeHandler extends SimpleTreeHandler {
		
		private ProductCatalogTree tree;
		
		public ProductCatalogTreeHandler(ProductCatalogTree tree){
			ProductCatalogTreeHandler.this.tree = tree;
		}
		
		@Override
		public SimpleTreeNode createRoot() {
			return new SimpleTreeNode("HiddenRootNotAvailableToUsers");
		} 
		
		@Override
		public void onNodeSelected(SimpleTreeNode node) {
			ProductCatalogTree catalogTree = ProductCatalogTreeHandler.this.tree;
			if (node == null || !(node instanceof ProductCatalogTreeNode)) {
				catalogTree.setSelectedCalalog(null);
				return;
			}
			
			ProductCatalogTreeNode catalogNode = (ProductCatalogTreeNode) node;
			catalogTree.setSelectedCalalog(catalogNode.getProductCatalog());
			DataCallback<ProductCatalog> action = catalogTree.getAction();
			if (action != null){
				action.onCompleted(catalogNode.getProductCatalog());
			}
		}
		
	}
}
